﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.BasicIntefaces;
using SharpBolt.Basics;
using IrrKlang;

namespace TestGame.SoundUtilites
{
    class KlangSoundRender : ISoundRender
    {
        ISoundEngine engine;

        public KlangSoundRender()
        {
            engine = new IrrKlang.ISoundEngine();
        }

        public bSoundSource GetSoundSource(string name)
        {
            bSoundSource soundSource = new bSoundSource();
            ISoundSource irrSoundSource = engine.GetSoundSource(name, true);
            soundSource.data = irrSoundSource;
            soundSource.name = name;
            soundSource.length = irrSoundSource.PlayLength;
            return soundSource;
        }
        public void FreeSoundSource(string name)
        {
            engine.RemoveSoundSource(name);
        }

        public bSound GetEmmitter(bSoundSource source)
        {
            ISound irrSound = engine.Play2D(source.data as ISoundSource, false, true, false);
            bSound sound = new bSound();
            sound.source = source;
            sound.data = irrSound;
            return sound;
        }

        public void FreeEmmitter(bSound sound)
        {

        }

        public void Play(bSound sound, bool loop = false)
        {
            ISound irrSound = (sound.data as ISound);
            irrSound.PlayPosition = 0;
            irrSound.Paused = false;
        }

        public void Stop(bSound sound)
        {
            (sound.data as ISound).Stop();
        }

        public void Pause(bSound sound)
        {
            (sound.data as ISound).Paused = true;
        }

        public void Continue(bSound sound)
        {
            (sound.data as ISound).Paused = false;
        }

        public void SetVolume(bSound sound, double volume)
        {
            (sound.data as ISound).Volume = (float)volume;
        }

        public void SetGlobalVolume(double globalVolume)
        {
            engine.SoundVolume = (float)globalVolume;
        }


        public bool IsStopped(bSound sound)
        {
           return (sound.data as ISound).Finished;
        }
    }
}
